package ahut2020.day14;

//多项式控件
public class Queen8b {
    //递归程序
    static int n = 8;
    static int[] book = new int[n];
    static int[] arr = new int[n];


    static void dfs(int k) {
        if (k >= n) {
            //System.out.println(Arrays.toString(arr));
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (j == arr[i])
                        System.out.print(" Q ");
                    else
                        System.out.print(" . ");
                }
                System.out.println();
            }
            System.out.println("-------------------------");
            return;
        }
        for (int i = 0; i < n; i++) {
            if (book[i] == 1) continue;
            //put Q0~Q[k-1]
            //Q[k]=(k,i)
            boolean flag = true;
            for (int j = 0; j < k; j++) {
                if (Math.abs(i - arr[j]) == (k - j)) {
                    flag = false;
                    break;
                }
            }
            if (!flag) continue;
            arr[k] = i;
            book[i] = 1;
            dfs(k + 1);
            book[i] = 0;
        }
    }

    public static void main(String[] args) {
        dfs(0);
    }
}
